package player

import (
	"GameServer/Common/utils"

	"github.com/globalsign/mgo/bson"
	log "github.com/sirupsen/logrus"

	"GameServer/Common/data"
	"GameServer/Game/errors"
	"GameServer/Game/sp"
	"GameServer/Game/union"
)

// 工会玩家聊天
func (p *Player) ChatRequest(req *sp.ChatRequest) (res *sp.ChatResponse, err error) {
	// 发言后，5秒钟之内，不能说话。
	if v, ok := p.GetData("unionChatNextTime"); ok {
		if utils.TNow().Unix() < v.(int64) {
			res = &sp.ChatResponse{
				Errcode: data.Errcode.TooFast,
			}
			log.WithFields(log.Fields{
				"uid":    p.User.Uid.Hex(),
				"method": "ChatRequest",
			}).Debug("玩家说话速度过快")
			return
		}
	}

	// 发言后，设置下次说话时间。
	p.PutData("unionChatNextTime", utils.TNow().Unix()+5)

	mid := bson.NewObjectId().Hex()
	switch req.Channel {
	case 1:
		err = union.UnionModule.UnionChat(mid, p.User, req.Msg, req.Ext, BroadData)
		if err != nil {
			res = &sp.ChatResponse{
				Errcode: errors.ErrCode(err),
			}
			return
		}
		res = &sp.ChatResponse{Mid: mid}
	default:
		return
	}
	return
}
